// pages/order/info/index.js
const app = getApp()
import http from '../../../utils/request.js';
import alert from '../../../utils/alert.js';
import { $wuxSelect } from '../../../wux/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderNo:'',
    order:{},
    payConfig:{},
    ordergoods:[],
    vipCardList:[],
    couponList:[],
    couponTypes:[],
    discountList:[],
    payList:[],
    useCoupon:{},
    couponPopup:false,
    couponTypeIndex:0,
    sum:0,
    cardIndex:0,
    scrollHeight:'500px'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var res = wx.getSystemInfoSync();
    this.setData({
      orderNo:options.order_no,
      scrollHeight: res.windowHeight - 46 + 'px'
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getPayConfig()
    this.getOrderGoods(this.data.orderNo)
    this.getOrderMoneyInfo(this.data.orderNo)
    this.getOrderInfo(this.data.orderNo)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getOrderInfo: function (order_no){
    http.get(
      app.config.service.getOrderInfo,
      { app_id: app.globalData.app_id, order_no: order_no }
    ).then(data => {
      wx.setNavigationBarTitle({
        title:data.table_name+'的订单'
      })
      this.setData({
        order:data,
      })
    }, err => {
      alert.alert(err)
    });
  },
  getOrderGoods:function(order_no){
    http.get(
      app.config.service.getOrderGoods,
      { app_id: app.globalData.app_id, order_no: order_no }
    ).then(data => {
      this.setData({
        ordergoods: data.goods,
        sum:data.sum
      })
    }, err => {
      alert.alert(err)
    });
  },
  getOrderMoneyInfo:function(order_no){
    http.get(
      app.config.service.getOrderMoneyInfo,
      {order_no: order_no }
    ).then(data => {
      this.setData({
        discountList: data.discount,
        payList: data.pay
      })
    }, err => {
      alert.alert(err)
    });
  },
  selectVipCard:function(){
    http.get(
      app.config.service.getMyCard,
      {is_vip:1}
    ).then(data => {
      data.forEach(function(value){
        value.title='卡号:'+value.card_no+'(余额:'+value.money+')'
        value.value=value.card_no
      })
      $wuxSelect('#card-select').open({
        value: data[0].card_no,
        options: data,
        onConfirm: (value, index, options) => {
          if (index !== -1) {
            this.setOrderVipCard(value)
          }
        },
      })

    }, err => {
      alert.alert(err)
    });
  },
  setOrderVipCard:function(card_no){
    http.post(app.config.service.setOrderVipCard, {
      app_id: app.globalData.app_id,
      order_no: this.data.orderNo,
      card_no:card_no
    }).then(data => {
      console.log(data)
      this.getOrderMoneyInfo(this.data.orderNo);
      this.setData({
        order:data
      })
    }, err => {
      alert.alert(err)
    });
  },
  goPay:function(){
    wx.navigateTo({
      url: '../pay/index?order_no='+this.data.orderNo,
    })
  },
  useCoupon:function(e){
    var order = this.data.order;
    http.get(
      app.config.service.getOrderUseCoupon,
      {
        order_no: order.order_no,
        total: order.total,
        balance:order.balance
      }
    ).then(data => {

      if (data.couponList.length==0){
        alert.alert('无可用优惠券');
      }else{
        this.setData({
          couponList: data.couponList,
          couponTypes: data.coupons,
          couponPopup: true
        })
      }
      this.getOrderMoneyInfo(this.data.orderNo);
    }, err => {
      this.setData({
        couponPopup: false
      })
      alert.alert(err)
    });
  },
  
  couponSubmit: function (e) {
    var coupon = this.data.couponList[this.data.couponTypeIndex]
      if (e.detail.value.use_num > coupon.single_use_num) {
        alert.alert("一个订单只能使用数目为:" + coupon.single_use_num)
        return false
      }

      if (coupon.coupon_num < e.detail.value.use_num) {
        alert.alert('此优惠券持有数目:' + coupon_num)
        return false
      }

      if (coupon.use_type == "mutex" && e.detail.value.use_num > 1) {
        alert.alert("检测到此优惠券是互斥券只能使用一个")
        return false
      }

      var params = {
        app_id:app.globalData.app_id,
        use_num: e.detail.value.use_num,
        pay_account: coupon.coupon_id,
        order_no: this.data.orderNo,
        amount: e.detail.value.use_coupon_offset_fee,
        pay_type: "store",
        pay_channel: "coupon",
        coupon_type_id: coupon.coupon_type_id
      }

    http.post(app.config.service.goOrderPay, params).then(data => {
      alert.alert(data.desc)
      this.setData({
        couponPopup: false
      })
      this.getOrderMoneyInfo(this.data.orderNo)
      }, err => {
        alert.alert(err)
      });
  },
  
  bindCouponTypeChange: function (e) {
    var couponTypes = this.data.couponTypes;
    var couponList = this.data.couponList
    this.setData({
      couponTypeIndex: e.detail.value,
      useCoupon: couponList[e.detail.value]
    })
  },
  onCouponPopup:function(e){
    this.setData({
      couponPopup:false
    })
  },
  goCardPay:function(e){
    wx.navigateTo({
      url: '../../pay/card/index?order_no='+this.data.orderNo,
    })
  },
  goWechatPay: function (e) {
    wx.navigateTo({
      url: '../../pay/wechat/index?order_no=' + this.data.orderNo,
    })
  },
  goAliPay: function (e) {
    wx.navigateTo({
      url: '../../pay/alipay/index?order_no=' + this.data.orderNo,
    })
  },
  goPayLog:function(){
    wx.navigateTo({
      url: '../../pay/log/index?order_no=' + this.data.orderNo,
    })
  },
  getPayConfig(){
    http.get(
      app.config.service.getPayConfig
    ).then(data => {
      this.setData({
        payConfig: data,
      })
    }, err => {
      alert.alert(err)
    });
  }
})